package org.jet.emall.prod.controller;

import org.jet.emall.common.restful.result.CommonResult;
import org.jet.emall.prod.CustomerVoTransfer;
import org.jet.emall.prod.dto.CustomerAddrDTO;
import org.jet.emall.prod.dto.CustomerDetailsDTO;
import org.jet.emall.prod.dto.NewCustomerDTO;

import org.jet.emall.prod.model.NewCustomerModel;
import org.jet.emall.prod.model.UpdateCustomerAddrModel;
import org.jet.emall.prod.service.CustomCustomerService;
import org.jet.emall.prod.vo.CustomerAddrVO;
import org.jet.emall.prod.vo.CustomerDetailsVO;
import org.jet.emall.prod.wrapper.CustomerIdAndAddrIdWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

import java.util.List;

/**
 * @author xiaozai
 * @version 1.0
 * @date 2020-04-23 11:32
 */

@RestController
public class CustomerController {

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Autowired
    private CustomCustomerService customCustomerService;

    @RequestMapping(value = "/customer",method = RequestMethod.GET)
    public CommonResult<CustomerDetailsVO> getCustomer(@PathVariable Long id){
        CustomerDetailsDTO dto = customCustomerService.getCustomer(id);

        CustomerDetailsVO vo =  CustomerVoTransfer.INSTANCE.toVo(dto);
        return CommonResult.success(vo,"获取用户详细信息成功");
    }


    @RequestMapping(value = "/customer",method = RequestMethod.POST)
    public CommonResult<Object> register(@RequestBody @Valid NewCustomerModel customer){
        NewCustomerDTO dto = CustomerVoTransfer.INSTANCE.toDto(customer);
        //将密码加密
        dto.setPassword(passwordEncoder.encode(dto.getPassword()));
        customCustomerService.save(dto);
        return CommonResult.created("注册用户成功");
    }


    @RequestMapping(value = "/customer/addrs/{customerId}",method = RequestMethod.GET)
    public CommonResult<List<CustomerAddrVO>> listAddrs(@PathVariable Long customerId){
        List<CustomerAddrDTO> dtoList = customCustomerService.listAddrs(customerId);
        if(dtoList==null || dtoList.isEmpty()){
            return CommonResult.notFound("暂时还没有添加地址,获取不到列表");
        }
        return (CommonResult<List<CustomerAddrVO>>)(Object)CommonResult.success(dtoList,"获取收货地址列表成功");
    }

    @RequestMapping(value = "/customer/addrs",method = RequestMethod.POST)
    public CommonResult<List<CustomerAddrVO>> addAddr(@RequestBody CustomerAddrVO vo){
        CustomerAddrDTO dto = CustomerVoTransfer.INSTANCE.toAddrDto(vo);
        customCustomerService.saveAddr(dto);
        List<CustomerAddrDTO> dtoList = customCustomerService.listAddrs(vo.getCustomerId());
        if(dtoList==null || dtoList.isEmpty()){
            return CommonResult.notFound("暂时还没有添加地址,获取不到列表");
        }
        return (CommonResult<List<CustomerAddrVO>>)(Object)CommonResult.success(dtoList,"添加收货地址成功");
    }

    @RequestMapping(value = "/customer/addrs",method = RequestMethod.PUT)
    public CommonResult<List<CustomerAddrVO>> updateAddr(@RequestBody UpdateCustomerAddrModel model){
        CustomerAddrDTO dto = CustomerVoTransfer.INSTANCE.toAddrDto(model);
        customCustomerService.updateAddr(dto);
        List<CustomerAddrDTO> dtoList = customCustomerService.listAddrs(model.getCustomerId());
        if(dtoList==null || dtoList.isEmpty()){
            return CommonResult.notFound("暂时还没有添加地址,获取不到列表");
        }
        return (CommonResult<List<CustomerAddrVO>>)(Object)CommonResult.success(dtoList,"修改收货地址成功");
    }

    @RequestMapping(value = "/customer/addrs",method = RequestMethod.PATCH)
    public CommonResult<List<CustomerAddrVO>> updateDefaultAddr(@RequestBody @Valid CustomerIdAndAddrIdWrapper wrapper){
        customCustomerService.updateDefaultAddr(wrapper.getCustomerId(),wrapper.getAddrId());
        List<CustomerAddrDTO> dtoList = customCustomerService.listAddrs(wrapper.getCustomerId());
        if(dtoList==null || dtoList.isEmpty()){
            return CommonResult.notFound("暂时还没有添加地址,获取不到列表");
        }
        return (CommonResult<List<CustomerAddrVO>>)(Object)CommonResult.success(dtoList,"设置默认收货地址成功");
    }

}
